/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sample.struts2;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.validator.annotations.Validations;
import com.opensymphony.xwork2.validator.annotations.VisitorFieldValidator;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

/**
 *
 * @author Thai Ha Cong
 */
//@Validations
@Results({
    @Result(name = "success", location = "/welcome.jsp"),
    @Result(name = "error", location = "/invalid.jsp"),
    @Result(name = "input", location = "/index.jsp")
})

public class LoginAction extends ActionSupport implements ModelDriven<User>{
    private User user = new User();
    
    public LoginAction() {
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    @Override
    public String execute() throws Exception {
//        String result = "fail";
        
        if (user.getUsername().equals("thaiha") && user.getPassword().equals("1")) {
            System.out.print("--------------------------------------success");
            return SUCCESS;
        }
        
        System.out.print("--------------------------------------fail");        
        return ERROR;
    }
    
//    @Override
//    public void validate() {
//        if (this.user.getUsername().length() == 0) {
//            addFieldError("username", "Username is required.");
//        } else if (this.user.getUsername().length() < 6) {
//            addFieldError("username", "Username is too short.");
//        }
//
//        if (this.user.getPassword().length() == 0) {
//            addFieldError("password", "Password is required.");
//        }
//    }

    @Override
    @VisitorFieldValidator(message = "", appendPrefix = false)
    public User getModel() {
        return user;
    }
    
}